package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.view.MyDialog;

public class WordBookForListAdapter extends BaseAdapter {

	List<Integer> list;
	Context mContext;
	WordFactory wf;
	boolean isShowPublish = false; // 是否显示出版社
	boolean isShowBookImg = false; // 是否显示图书封面
	boolean isJunior = false; // 是否是初中配套
	int width, height;
	int bookIndex, unlearnWordCount;
	String bookNameStr;
	MyHolder myHolder;
	LayoutInflater layoutInflater;

	public WordBookForListAdapter(List<Integer> list, Context mContext, boolean isShowPublish, boolean isShowBookImg, boolean isJunior) {
		this.list = list;
		this.mContext = mContext;
		this.isShowPublish = isShowPublish;
		this.isShowBookImg = isShowBookImg;
		this.isJunior = isJunior;
		wf = WordFactory.getInstance(mContext);
		width = Utils.getScreenWidth(mContext) / 6;
		height = Utils.getScreenHeight(mContext) / 8;
		layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setSelection(int position) {
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		long start = System.currentTimeMillis();
		bookIndex = list.get(position);

		// if(convertView==null){
		myHolder = new MyHolder();
		View view = layoutInflater.inflate(R.layout.view_book_item, null);
		myHolder.bookName = (TextView) view.findViewById(R.id.book_name);
		myHolder.letterNum = (TextView) view.findViewById(R.id.book_count);
		myHolder.price = (TextView) view.findViewById(R.id.bean_count);
		myHolder.bookImg = (ImageView) view.findViewById(R.id.book_img);
		myHolder.bookItemLayout = (RelativeLayout) view.findViewById(R.id.book_item_layout);
		myHolder.lookLayout = (TextView) view.findViewById(R.id.look_layout);
		// convertView.setTag(myHolder);
		// }else{
		// myHolder = (MyHolder) convertView.getTag();
		// }

		myHolder.bookName.setVisibility(View.VISIBLE);

		if (isShowPublish) {
			if (isJunior) {
				myHolder.bookName.setText(AppPreference.Book.PUBLISH_NAME_JUNIOR[bookIndex]);
			} else {
				myHolder.bookName.setText(AppPreference.Book.PUBLISH_NAME_SENIOR[bookIndex]);
			}

			myHolder.letterNum.setVisibility(View.GONE);
		} else {
			bookNameStr = wf.getBookName(AppPreference.Book.BOOK_ID[bookIndex]);
			myHolder.bookName.setText(bookNameStr);
			unlearnWordCount = wf.getUnLearnedWordCount(bookIndex);
			String str;
			if (unlearnWordCount == 0)
				str = "已完成";
			else
				str = unlearnWordCount + "/" + wf.getWordCountInBook(bookIndex);
			myHolder.letterNum.setText(str);

			int price = wf.getPrice(bookIndex);
			if (price > 0) {
				myHolder.price.setVisibility(View.VISIBLE);
				myHolder.lookLayout.setVisibility(View.VISIBLE);
				if (wf.isBought(bookIndex)) {
					myHolder.price.setText("已购买");
				} else {
					myHolder.price.setText(price + "");
				}
			} else {
				myHolder.lookLayout.setVisibility(View.GONE);
			}
		}

		if (isShowBookImg) {
			myHolder.bookImg.setVisibility(View.VISIBLE);
			myHolder.bookImg.setImageResource(AppPreference.Book.BOOK_IMG[bookIndex]);
			LayoutParams params = myHolder.bookImg.getLayoutParams();
			params.width = width;
			params.height = height;
			myHolder.bookImg.setLayoutParams(params);
		}

		myHolder.lookLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showTop50WordsDialog(list.get(position));
			}
		});

		LogUtil.d("用时：" + (System.currentTimeMillis() - start));
		return view;
	}

	protected void showTop50WordsDialog(int bookIndex1) {
		ArrayList<OneWord> top50Words = wf.getTop50Words(bookIndex1);

		// final Dialog dialog = new AlertDialog.Builder(mContext).create();

		View dialogView = LayoutInflater.from(mContext).inflate(R.layout.view_top_50_dialog, null);

		MyDialog dialog = new MyDialog(mContext, dialogView, R.style.mydialog, false);

		TextView titleName = (TextView) dialogView.findViewById(R.id.title_name);
		titleName.setText(wf.getBookName(AppPreference.Book.BOOK_ID[bookIndex1]) + "预览");
		ListView lv = (ListView) dialogView.findViewById(R.id.word_lv);

		TextView bottomTv = new TextView(mContext);
		bottomTv.setText("预览部分已结束(50/" + wf.getUnLearnedWordCount(bookIndex1) + ")");
		AbsListView.LayoutParams al = new AbsListView.LayoutParams(android.widget.AbsListView.LayoutParams.MATCH_PARENT, android.widget.AbsListView.LayoutParams.WRAP_CONTENT);
		bottomTv.setLayoutParams(al);
		bottomTv.setPadding(0, Utils.changeDipToPx(mContext, 10), 0, Utils.changeDipToPx(mContext, 10));
		bottomTv.setGravity(Gravity.CENTER_HORIZONTAL);
		bottomTv.setTextSize(18);
		if (NightModeUtil.isNightModeNow()) {
			bottomTv.setTextColor(mContext.getResources().getColor(R.color.word_color_night));
		} else {
			bottomTv.setTextColor(mContext.getResources().getColor(R.color.black));
		}
		lv.addFooterView(bottomTv);

		WordListForPhraseAdapter wordListAdapter = new WordListForPhraseAdapter(mContext, (ArrayList<OneWord>) top50Words);
		lv.setAdapter(wordListAdapter);

		dialog.show();
		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams wl = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		int width = Utils.getScreenWidth(mContext) * 6 / 7;
		int height = Utils.getScreenHeight(mContext) * 11 / 14;
		wl.width = width;
		wl.height = height;
		dialogWindow.setAttributes(wl);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(dialogView);
	}

	class MyHolder {
		TextView bookName, letterNum, price, lookLayout;
		ImageView bookImg;
		RelativeLayout bookItemLayout;
	}
}
